/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.ui.util;

import kz.kstu.ent.desktop.ui.util.comparator.*;

import java.util.*;

/**
 * @author Oleg Anastassov
 */
public final class SortHelper {

    private SortHelper() {
    }

    /**
     * Sorts table data by given column index.
     *
     * @param data          data for sorting
     * @param columnIndex   column index
     * @param sortAscending sort mode (if true - ascending, otherwise - descending)
     * @param classType     type of column data
     */
    public static void sortByColumn(List<Object[]> data, int columnIndex, boolean sortAscending, Class<?> classType) {
        Comparator<Object[]> comparator = null;
        if (classType.equals(Date.class)
                || (classType.getSuperclass() != null && classType.getSuperclass().equals(Date.class))) {
            comparator = new DateComparator<Object[]>(columnIndex, sortAscending);
        } else if (classType.equals(String.class) || classType.equals(StringBuilder.class)
                || classType.equals(StringBuffer.class)) {
            comparator = new StringComparator<Object[]>(columnIndex, sortAscending);
        } else if (classType.equals(Boolean.class)) {
            comparator = new BooleanComparator<Object[]>(columnIndex, sortAscending);
        } else if (classType.getSuperclass() != null && classType.getSuperclass().equals(Number.class)) {
            comparator = new NumberComparator<Object[]>(columnIndex, sortAscending);
        } else {
            comparator = new DefaultComparator<Object[]>(columnIndex, sortAscending);
        }
        sortByColumn(data, comparator);
    }

    /**
     * Sorts table data using given comparator.
     *
     * @param data       data for sorting
     * @param comparator specified comparator
     */
    private static void sortByColumn(List<Object[]> data, Comparator<Object[]> comparator) {
        if (data != null) {
            Collections.sort(data, comparator);
        }
    }

    /**
     * Converts two-dimensional array of object to ArrayList of one-dimensional array of object.
     *
     * @param data data for converting
     * @return converted data
     */
    public static List<Object[]> convertToArrayList(Object[][] data) {
        List<Object[]> convertedData = new ArrayList<Object[]>();
        if (data != null) {
            for (int i = 0; i < data.length; i++) {
                convertedData.add(data[i]);
            }
        }
        return convertedData;
    }
}
